package net.zjitc.controller;

import net.zjitc.pojo.Admin;
import net.zjitc.service.AdminService;
import net.zjitc.utils.APIResponse;
import net.zjitc.utils.JwtUtil;
import net.zjitc.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
//    @RequestMapping("/login")
//    public String login(Admin admin, Model model, HttpSession session){
//
//        Admin admin1 = adminService.findAdmin(admin.getUsername(),admin.getPassword());
//        if (admin1!=null){
//            model.addAttribute("msg","查询成功");
//            session.setAttribute("admin",admin);
//            return "index";
//        }
//        model.addAttribute("login_msg","用户名或密码错误");
//        return "login";
//    }








    @PostMapping("login")
    @ResponseBody
    @CrossOrigin("*")
    public APIResponse<Token> login(@RequestBody Admin admin){

        Admin admin1 = adminService.findAdmin(admin.getUsername(),admin.getPassword());
        APIResponse apiResponse = new APIResponse<>();
        apiResponse.setData(new Token());
        if (admin1!=null){
            String token = JwtUtil.generateToken(admin1.getUsername());
        apiResponse.APIResponseOK(new Token(token),"登录成功");
            return apiResponse;

        }else {
            apiResponse.APIResponseError(1000,null,"用户名或密码错误");
            return apiResponse;
        }

    }


}
